const express = require('express')
 // 用来解决 history 模式404问题
 const http=require('http');
 const multer  = require('multer');

const DBHelper = require('./utils/DBHelper');
let pool = new DBHelper().getpool();
let connection = new DBHelper().getConnection();

const sql = require('./sqlMap');

const app = express();
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true }))

var url = require('url');

const hostname = '127.0.0.1';
const port = 5005;


const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, 'upload/') // 确保这个文件夹已经存在
    },
    filename: function (req, file, cb) {
        // console.log(req.params);

        cb(null, file.originalname )
    }
})
const upload = multer({ storage: storage });



 app.get('/list', (request, response) => {
    // console.log(request)
    response.send({
        username: 'xfwang',
         pwd:'1'
   })
 })

 app.post('/login',(request,response)=>{
    let params = request.body;
    let sqlStr = sql.users.select;
    pool.getConnection(function(err, connection) {
        connection.query(sqlStr,[params.username,params.password], function(err, rows) {
            if (err) {
                 console.error(err);
                      return false; 
            } else {
                console.log(rows);
                let logined=false;
                if(rows.length)
                    logined=true;
                response.send({
                    login: logined
               })
            }

        });
        connection.release();

    });

 })

app.post('/fileExist',(request,response)=>{
    let sqlStr =sql.models.exist;
    let params = request.body;
    console.log(sqlStr,params);
    pool.getConnection(function(err, connection) {
        connection.query(sqlStr,[params.name], function(err, rows) {
            if (err) {
                 console.error(err);
                      return false; 
            } else {
                console.log(rows);
                let exist=false;
                if(rows.length)
                    exist=true;
                response.send({
                    exist: exist
               })
            }

        });
        connection.release();

    });

})

app.post('/models',(request,response)=>{
    let sqlStr = sql.models.select;
    console.log(sqlStr);
    // let pool = new DBHelper().getpool();
        pool.getConnection(function(err, connection) {
            connection.query(sqlStr,[], function(err, rows) {
                if (err) {
                     console.error(err);
                          return false; 
                } else {
                    console.log(rows);
                    response.json(rows);
                }

            });
            connection.release();

        });
})

app.post('/uploadFile',upload.array('file'),(req,res)=>{
    let sqlStr = sql.models.add;

        pool.getConnection(function(err, connection) {
            connection.query(sqlStr,[req.files[0].originalname,'upload/'+req.files[0].originalname,'wxf','1'], function(err, rows) {
                if (err) {
                     console.error(err);
                          return false; 
                } else {
                    console.log(rows);
                    res.json(rows);
                }

            });
            connection.release();

        });

})

app.listen(port ,hostname, (err) => {
//5005是服务器端口，可自己定，但是不要和一些软件的默认端口冲突
    if (!err) {
        console.log(`Server running at http://${hostname}:${port}/`);
  
    }
})